package sqlToGUI.charts.helperUtilities;


public class TelemetryDataEntry {
	
	/**Variable containing data retrieved by an algorithm from the
	 * SQl database */
	private double [] sqlBufferedData;
	
	/**Class constructor */
	public TelemetryDataEntry(int bufferSize) {
		/**TODO: AHH THIS HAS TO BE CHANGEDS*/
		sqlBufferedData = new double[bufferSize];
		for(int i = 0; i < sqlBufferedData.length; i++){
			sqlBufferedData[i] = 0;
		}
	}
	
    /**The listener list */
    protected javax.swing.event.EventListenerList listenerList =
        new javax.swing.event.EventListenerList();

    /**Register the event to the given listener */
    public void addTelemetryChartEventListener(TelemetryChartListener listener) {
        listenerList.add(TelemetryChartListener.class, listener);
    }

    /**Un0egister the event from the given listener */
    public void removeTelemetryChartEventListener(TelemetryChartListener listener) {
        listenerList.remove(TelemetryChartListener.class, listener);
    }

    /**Method called when data is to be updated */
    public void updateData(int index, double sqlInput){
    	sqlBufferedData[index] = sqlInput;
    	this.fireTelemetryChartEvent(new TelemetryChartEvent(this));
    }
    
    /**Return the latest data from SQL */
    public double [] getUpdate(){
    	return sqlBufferedData;
    }
    
    /**Method for firing the events */
    public void fireTelemetryChartEvent(TelemetryChartEvent evt) {
        Object[] listeners = listenerList.getListenerList();

        for (int i=0; i<listeners.length; i+=2) {
            if (listeners[i]==TelemetryChartListener.class) {
                ((TelemetryChartListener)listeners[i+1]).myEventOccurred(evt);
            }
        }
    }
}
